<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  
  <div id="example"></div>

  <script type="text/javascript" src="../js/react.development.js"></script>
  <script type="text/javascript" src="../js/react-dom.development.js"></script>
  <script type="text/javascript" src="../js/prop-types.js"></script>
  <script type="text/javascript" src="../js/babel.min.js"></script>
  
  <script type="text/babel">

    //App应用组件（最外层的组件）
    class App extends React.Component {
      state = {
        todoArr:['吃饭','睡觉','学习']
      }

      addTodo = (data) => {
        //获取原状态中的todo 数组
        let {todoArr} = this.state
        //向todo 数组中追加一条数据
        todoArr.unshift(data)
        //更新状态
        this.setState({todoArr:todoArr})
        
      }
      //状态在哪个组件里 更新状态的方法就在哪个组件里
      render(){
        let {todoArr} = this.state
        return (
        <div>
          <h1>Simple todo list</h1>
          <Add addTodo={this.addTodo}   count={todoArr.length}/>
          <List todoArr={todoArr}/>
          </div> 
        )
      }
    }
    //Add 组件-----用于添加一条数据
    class Add extends React.Component {
      add = ()=>{
        //输入函数体  1，获取用户输入 2. 校验数据 3.将用户的输入添加到状态里面
        let userInput = this.input.value
        if (userInput.trim() === '') return
        this.props.addTodo(userInput)
        //清空输入框
        this.input.value = ''

      }
      render(){
        let {count} = this.props
        return (
          <div>
            <input type="text" ref={(input)=>{this.input = input}}/>
            <button onClick = {this.add}>Add #{count}</button>
            </div>
          )
      }
    }
    //List 组件 -----------用于展示列表
    class List extends React.Component {
      
      render(){
        let {todoArr} = this.props
        return (
        <ul>
            {todoArr.map( (item,index) => <li key={index}>{item} </li>)}
            </ul>
        )
      }
    }
  
    ReactDOM.render(<App/>,document.getElementById('example'))
  </script>
</body>
</html>